﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using LeafBookmark.Helper;
using LeafBookmark.Data;
using System.Threading;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Input;

namespace LeafBookmark.ViewModels
{
    class BookmarkListViewModel : PropertiesViewModelBase
    {
        #region Commands
        public ICommand CheckDuplicateCommand
        {
            get
            {
                return new NormalCommand
                {
                    ExecuteDelegate = delegate
                    {
                        StartCheckDuplicate();
                    }
                };
            }
        }
        #endregion Commands

        #region Data

        private CollectionViewSource BookmarkCollectionViewSource
        {
            get;
            set;
        }

        private ICollectionView bookmarkCollectionView;
        public ICollectionView BookmarkCollectionView
        {
            get
            {
                return bookmarkCollectionView;
            }

            private set
            {
                if (value != bookmarkCollectionView)
                {
                    bookmarkCollectionView = value;
                    RaisePropertyChanged("BookmarkCollectionView");
                }
            }
        }

        private BookmarkLibrary library;

        private int totalCount;
        public int TotalCount
        {
            get
            {
                if (null == BookmarkCollectionViewSource)
                    return 0;
                else
                    return (BookmarkCollectionViewSource.Source as IList<BookmarkViewModel>).Count;
            }
            set
            {
                if (value != totalCount)
                {
                    totalCount = value;
                    RaisePropertyChanged("TotalCount");
                }
            }
        }

        private int duplicatedCount;
        public int DuplicatedCount
        {
            get
            {
                return duplicatedCount;
            }
            set
            {
                if (value != duplicatedCount)
                {
                    duplicatedCount = value;
                    RaisePropertyChanged("DuplicatedCount");
                }
            }
        }

        #endregion Data

        public BookmarkListViewModel()
        {
            if (DesignerChecker.IsInDesigner)
                CreateDummyDataForDesigner();
            else
            {
                StartGetBookmarks();
            }
        }

        private void CreateDummyDataForDesigner()
        {
            List<BookmarkViewModel> bookmarkLibrary = new List<BookmarkViewModel>();
            for (int i = 0; i < 20; i++)
            {
                BookmarkViewModel bookmark = new BookmarkViewModel("test", "http://xxx.xx.xxx");
                bookmark.IsKey = true;
                bookmarkLibrary.Add(bookmark);
            }

            BookmarkCollectionViewSource = new CollectionViewSource { Source = bookmarkLibrary };
            BookmarkCollectionView = BookmarkCollectionViewSource.View;
        }

        private void StartGetBookmarks()
        {
            ThreadPool.QueueUserWorkItem(GetBookmarks);
        }

        private void GetBookmarks(object state)
        {
            library = new BookmarkLibrary();
            library.GetAllBookmarks();
            this.Dispatcher.BeginInvoke(new Action(
                delegate
                {
                    IEnumerable<BookmarkViewModel> bookmarkList = library.BookmarkList.Select(file => new BookmarkViewModel(file)).ToList();
                    this.BookmarkCollectionViewSource = new CollectionViewSource { Source = bookmarkList };
                    this.BookmarkCollectionView = this.BookmarkCollectionViewSource.View;
                    this.TotalCount = bookmarkList.Count();

                }
                ));
        }

        private void StartCheckDuplicate()
        {
            // [TODO] Sorting in UI is pretty slow and might be cause UI not responding.
            // Consider use Linq query and implement a progress report, then run sorting in a work thread.
            SortDescription sortDescription = new SortDescription("Title", ListSortDirection.Ascending);
            BookmarkCollectionView.SortDescriptions.Add(sortDescription);

            PropertyGroupDescription groupDescription = new PropertyGroupDescription("Url");
            if(BookmarkCollectionView.CanGroup)
            {
                BookmarkCollectionView.GroupDescriptions.Add(groupDescription);
                this.DuplicatedCount = TotalCount - BookmarkCollectionView.Groups.Count;

                foreach (CollectionViewGroup group in BookmarkCollectionView.Groups)
                {
                    BookmarkViewModel bookmark = group.Items[0] as BookmarkViewModel;
                    if (null != bookmark)
                        bookmark.IsKey = true;
                }
            }
        }

    }
}
